package com.appkubes.fintech.restful.application.risk.data.xingpan.http;

import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class HttpService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private CloseableHttpClient httpClient;

    private HttpService() {
    }

    public static HttpService newInstance() {
        HttpService impl = new HttpService();
        impl.httpClient = HttpClients.custom().build();
        return impl;
    }

    public String execAndReturnString(HttpRequestBase request) {
        logger.info("Request url = {}", request.getURI());
        try {
            return httpClient.execute(request, new ReturnStringResponseHandler());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
